# -*- coding: utf-8 -*-

import pygame
import sys
import os

import ui
import ui.board
import ui.locals
import ui.tools

if __name__ == "__main__":
    # init
    # set windows size
    # TODO: to be as param
    size = width, height = 400, 600

    # initialize pygame and the screen
    pygame.init()

    screen = pygame.display.set_mode(size)

    # set up the background
    bg_img = ui.tools.load_image('background.png')

    screen.blit(bg_img[0], (0, 0))
    pygame.display.flip()

    #set up the game background

    # set up the picking board
    pick_board = ui.board.Picker()
    pick_board_surface = screen.subsurface((300, 50, 80, 160))
    pick_board.draw(pick_board_surface)

    # button
    button = pygame.draw.rect(screen, (255,255,255), (100,530,100, 20))

    # main board
    game_board = ui.board.Board()
    game_board_surface = screen.subsurface((50, 80, 200, 440))
    game_board.draw(game_board_surface)

    gameStatus = ui.locals.GAME
    # the main loop
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
            elif event.type == pygame.KEYDOWN and event.key == pygame.K_q: sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if gameStatus == ui.locals.GAME:
                    pos = pygame.mouse.get_pos()

                    # get absolute position of the picking board
                    pick_board_rect = pick_board_surface.get_rect()
                    (pick_board_rect.left, pick_board_rect.top) = pick_board_surface.get_offset()

                    if pick_board_rect.collidepoint(pos):
                        pick_board.onclick(pos, pick_board_surface.get_offset())
                        pick_board.draw(pick_board_surface)

                    # get absolute position of the game board
                    game_board_rect = game_board_surface.get_rect()
                    (game_board_rect.left, game_board_rect.top) = game_board_surface.get_offset()

                    if game_board_rect.collidepoint(pos):
                        game_board.onclick(pos, game_board_surface.get_offset(), pick_board.current)
                        game_board.draw(game_board_surface)

                    # check if button was clicked
                    if button.collidepoint(pos):
                        if game_board.check():
                            gameStatus = ui.locals.GAME_OVER
                        game_board.draw(game_board_surface)
                elif gameStatus == ui.locals.GAME_OVER:
                    screen.blit(bg_img[0], (0, 0))

                    # set up the picking board
                    pick_board = ui.board.Picker()
                    pick_board_surface = screen.subsurface((300, 50, 80, 160))
                    pick_board.draw(pick_board_surface)

                    # button
                    button = pygame.draw.rect(screen, (255,255,255), (100,530,100, 20))

                    # main board
                    game_board = ui.board.Board()
                    game_board_surface = screen.subsurface((50, 80, 200, 440))
                    game_board.draw(game_board_surface)

                    gameStatus = ui.locals.GAME

        pygame.display.flip()
